package elasticsearch;

import java.io.File;
import java.io.FileInputStream;
import java.net.UnknownHostException;

import demo.java8.Person;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import static java.net.InetAddress.getByName;

/**
 * Created by zhangyanfei on 2019/6/11.
 *
 * @author zhangyanfei
 * @date 2019/06/11
 */
public class ElasticsearchClient {
    public static TransportClient getTransportClient() {
        try {
            Settings settings = Settings.builder().put("cluster.name", "elasticsearch_zhangyanfei").build();
            TransportClient transportClient = new PreBuiltTransportClient(settings);
            transportClient.addTransportAddress(new InetSocketTransportAddress(getByName("127.0.0.1"), 9300));
            return transportClient;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public static void main(String[] args) throws Exception {
        Person person = new Person("");
        person.setAge(19);
        person.setId(1);
        person.setName("zhangyanfei");
        TransportClient client = getTransportClient();
        /**
         * create index
         */
        File file = new File("demo.json");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] fileContent = new byte[(int)file.length()];
        fileInputStream.read(fileContent);

        String contents = new String(fileContent, "UTF-8");

        client.admin().indices()
            .preparePutMapping("person_db").setType("student")
            .setSource(contents, XContentType.JSON).execute().actionGet();
        /**
         *  get document
         */
        GetResponse response = client.prepareGet("person_db", "student", "1").get();
        System.out.println(response.getSource());
        /**
         * search
         */
        SearchRequestBuilder srb1 = client
            .prepareSearch().setQuery(QueryBuilders.queryStringQuery("zhangyanfei")).setSize(1);
        SearchRequestBuilder srb2 = client
            .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "zhangyanfei")).setSize(1);

        MultiSearchResponse sr = client.prepareMultiSearch()
            .add(srb1)
            .add(srb2)
            .get();

        long nbHits = 0;
        for (MultiSearchResponse.Item item : sr.getResponses()) {
            SearchResponse responses = item.getResponse();
            nbHits += responses.getHits().getTotalHits();
            System.out.println(responses.toString());
        }
        System.out.println(nbHits);
    }
}
